package com.zzp.basepresenter.ui.popwindows;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzp.basepresenter.R;

public class BaseNoticePopWindow extends BasePopupWindow {

    private static BaseNoticePopWindow instance;
    private Button btnEnsure;
    private TextView textView, tv_title;
    private LinearLayout ll_parent;

    public static BaseNoticePopWindow getInstance(Activity context) {
        if (instance == null) {
            synchronized (BaseNoticePopWindow.class) {
                if (instance == null) {
                    instance = new BaseNoticePopWindow(context);
                }
            }
        }
        return instance;
    }

    public interface OnClickListener {
        void onClick();
    }

    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public BaseNoticePopWindow(Activity context) {
        super(context);
    }

    @Override
    public int getResView() {
        return R.layout.layout_pop_base_notice;
    }

    @Override
    public boolean transparentBackground() {
        return false;
    }

    @Override
    public boolean outTouchNotHide() {
        return true;
    }

    @Override
    public void findViewById(View view) {
        btnEnsure = view.findViewById(R.id.btn_ensure);
        textView = view.findViewById(R.id.tv_message);
        tv_title = view.findViewById(R.id.tv_title);
        ll_parent = view.findViewById(R.id.ll_parent);
        ll_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        btnEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick();
                }
                dismiss();
            }
        });
    }

    @Override
    public int setWidth() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    @Override
    public int setHeight() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    @Override
    public int setAnimationStyle() {
        return 0;
    }

    @Override
    public void show(Activity context, View view) {
        this.context = context;
        this.update();
        if (context.isFinishing()) {
            return;
        }
        if (view.getWindowToken() == null) {
            return;
        }
        tv_title.setText("");
        textView.setText("");
        //以某个控件的x和y的偏移量位置开始显示窗口
        showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }

    public void setMessage(String message) {
        textView.setText(message);
    }
}
